<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/page/public/taglib.jsp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>用户管理</title>
 <%@ include file="/WEB-INF/page/public/contentmeta.jsp"%>
  <link rel="stylesheet" href="/plugins/font-awesome/css/font-awesome.min.css">
   <link rel="stylesheet" href="/css/global.css" media="all" />
    <link rel="stylesheet" href="/css/tab.css" media="all" />
</head>
<body>

<div class="admin-tab admin-user">
    <div class="tab-condition">
        <form class="layui-form clefix"  action="/user/getuserlist" method="post" id="userlistform">
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <input type="text" name="keyWord" value="${pager.params.get('keyWord')}"  lay-verify="title" autocomplete="off" placeholder="请输入名字" class="layui-input">
                </div>
            </div>
            
            <div class="layui-form-item department">
				<div class="layui-input-inline">
					<select   name="deptid">
						<option value="">所在部门</option>
						<dic:dp value="${pager.params.get('deptid')}" type="2"/>
					</select>
				</div>
			</div>
            <div class="layui-form-item">
            	<input name="skipToPage" id="skipToPage"  type="hidden" value="${pager.pageNo}">
				<input type="hidden" id="pageNo" name="pageNo" value="${pager.pageNo}">
				<input type="hidden" id="totalPage" name="totalPage" value="${pager.totalPages}">
                <button class="layui-btn" lay-submit="" lay-filter="demo2"><i class="layui-icon layui-unselect layui-tab-close">&#xe615;</i>搜索</button>
            </div>
        </form>
        <div class="TM-tab-info">
        	<a class="layui-btn layui-btn-primary layui-btn-small adduser">+添加用户</a>
            <a class="layui-btn layui-btn-primary layui-btn-small active rolebind">+角色绑定</a>
            <a class="layui-btn layui-btn-primary layui-btn-small deluser">+删除用户</a>
            <a class="layui-btn layui-btn-primary layui-btn-small resetpwd">+重置密码</a>
        </div>
    </div>
    <div class="layui-field-box">
        <table class="site-table table-hover TM-user">
            <thead>
            <tr>
                <th style="width:10%;"><input type="checkbox" id="selected-all"/>姓名</th>
                 <th style="width:10%;">账号</th>
                <th style="width:10%;">拥有角色</th>
                <th style="width:10%;">拥有行业</th>
                <th style="width:5%;">部门</th>
                <th style="width:5%;">生日</th>
                <th style="width:10%;">身份证号码</th>
                <th style="width:10%;">户籍地址</th>
                <th style="width:5%;">学历</th>
                <th style="width:10%;">现住址</th>
                <th style="width:10%;">联系电话</th>
                <th style="width:5%;">操作</th>
            </tr>
            </thead>
            <tbody>
             
            	<c:forEach var="userlist" items="${pager.resultList}">
            	 <tr>
	                <td><input name="checkbox" type="checkbox"   value="${userlist.uid}"/>${userlist.name}</td>
	                <td>${userlist.account}</td>
	                <td>${userlist.rname}</td>
	                <td><c:forEach var="cal" items="${userlist.callinglist}">
						<dic:cf name="calling_category" type="1"
					value="${cal}" />
					</c:forEach></td>
	                <td><dic:dp value="${userlist.deptid}" type="1"/></td>
	                <td>${userlist.birthday}</td>
	                <td>${userlist.cardid}</td>
	                <td>${userlist.address}</td>
	                <td> 
		                <c:if test="${userlist.education==0}">未填</c:if>
						<c:if test="${userlist.education==1}">初中</c:if>
						<c:if test="${userlist.education==2}">高中</c:if>
						<c:if test="${userlist.education==3}">中专</c:if>
						<c:if test="${userlist.education==4}">大专</c:if>
						<c:if test="${userlist.education==5}">本科</c:if>
						<c:if test="${userlist.education==6}">硕士</c:if>
						<c:if test="${userlist.education==7}">博士</c:if> 
					</td>
	                <td>${userlist.currentaddr}</td>
	                <td>${userlist.phone}</td>
	                <td><button class="layui-btn layui-btn-primary layui-btn-small upduser"  data="${userlist.uid}"><i class="layui-icon "></i></button></td>
	            </tr>
			</c:forEach>
            </tbody>
        </table>
        <div class="admin-table-page">
            <div id="page" class="page"></div>
        </div>	
    </div>
</div>

 


<div class="layui-form-item" id="validatebound-formDiv" style="width:90%;margin-top:20px;display:none">
	<form  class="layui-form clefix" action="" id="validatebound-form" method="post">
		<div class="layui-form-item">
		    <label class="layui-form-label">角色</label>
		    <div class="layui-input-block">
		          	<select id="rolename" name="rolename" lay-filter="rolename"   > 				
					  <option value="">角色</option>
					</select>
		    </div>
		  </div>
		   <div class="layui-form-item">
	    	<div class="layui-input-block">
	     		 <button class="layui-btn" lay-submit  lay-filter="rolebindsubmit">保存</button>
		    </div>
		  </div>  
	</form>
</div>

 
<script>
 
    layui.use(['icheck','element','laypage','form'], function() {
        var $ = layui.jquery,
                laypage = layui.laypage,
                element = layui.element(); //Tab的切换功能，切换事件监听等，需要依赖element模块
         var form = layui.form();
        $('input').iCheck({
            checkboxClass: 'icheckbox_flat-blue	'
        });
 
    	$('.adduser').on('click', function() {
    		parent.layer.open({
				title: '基本信息',
				maxmin: true,
				type: 2,
				content: '/user/userinfopage',
				area: ['96%', '97%']
			});
		});
    	
    	$('.upduser').on('click', function() {
    		var id = $(this).attr("data");
			parent.layer.open({
				title: '基本信息',
				maxmin: true,
				type: 2,
				content: '/user/userinfopage?id='+id,
				area: ['96%', '97%']
			});
		});
    	
		
                
    	$('.rolebind').on('click', function() {
    		id = getCheckedId();
    	  	
    		boundcld();
    		layer.open({
				title: '角色绑定',
				maxmin: true,
				type: 1,
				content:  $('#validatebound-formDiv'),
				area: ['500px', '400px']
			});
    	 	form.render('select'); 
    	});        
                
    	$('.deluser').on('click', function() {
    		id = getCheckedId();
    	  	if(id.length<1){
    	  		   layer.msg("请选择用户！");  
    	  		   return ;
    	  	}
    	  	deleteuser();
    		 
    	});
    	
    	//重置密码
    	$('.resetpwd').on('click',function() {
    		id = getCheckedId();
    		if (id.length<1){
    			layer.msg("请选择用户！");
    		}
    		resetpwd(id);
    	});
                	
    	
    	
                
    	var pageNo = $("#pageNo").val();
    	var totalPage = $("#totalPage").val(); 
        //page
        laypage({
            cont: 'page',
            curr: pageNo//'${pager.pageNo}'
            ,pages: totalPage//'${pager.totalPages}' //总页数
            ,
            groups: 5 //连续显示分页数
            ,
            jump: function(obj, first) {
                //得到了当前页，用于向服务端请求对应数据
                var curr = obj.curr;
                if(!first) {
                	$("#skipToPage").val(curr);
                	$("#userlistform").submit();
                }
            }
        });
        
        
        $('.site-table tbody tr').on('click', function(event) {
            var $this = $(this);
            var $input = $this.children('td').eq(0).find('input');
//				$input.on('ifChecked', function(e) {
//					$this.css('background-color', '#pink');
//				});
            $input.on('ifUnchecked', function(e) {
                $this.removeAttr('style');
            });
            $input.iCheck('toggle');
        }).find('input').each(function() {
            var $this = $(this);
            $this.on('ifChecked', function(e) {
                $this.parents('.pink').css('background-color', '#f9eff1');
            });
            $this.on('ifChecked', function(e) {
                $this.parents('.yellow').css('background-color', '#f3fae3');
            });
            $this.on('ifUnchecked', function(e) {
                $this.parents('tr').removeAttr('style');
            });
        });
        $('#selected-all').on('ifChanged', function(event) {
            var $input = $('.site-table tbody tr td').find('input');
            $input.iCheck(event.currentTarget.checked ? 'check' : 'uncheck');
        });
    });
    
    
    
    
    function getcout(i,id) {
  	  var cSel = "";
  	  if(null==i||""==i){
  		layer.msg("用户未完善部门信息,无法分配！");
  	  }else{
  		  $.ajax({
  				type : "POST",
  				url : "/user/getuserdtpidlist",
  				data : {
  					deptid:i
  				},
  				dataType : "json",
  				async:false,
  				success : function(data) {
  					for(var i=0;i<data.length;i++){
  						cSel += "<option value='"+data[i].aid+"'>"+data[i].name+"</option>"
  					}
  				}
  			});
  		  $("#username").html(cSel);
  		  $("#creatid").val(id);
  		$("#myModal4").modal("show");
  	  }
  	
  } 
    
   function updatecust(){
  		 var updateid=$("#username").val();
  		 var creatid=$("#creatid").val();
  			$.ajax({
  				type : "POST",
  				url : "/user/updateusercout",
  				data : {
  					updateid:updateid,
  					creatid:creatid,
  				},
  				dataType : "json",
  				async:false,
  				success : function(data) {
  					if (data == 0) {
  						layer.msg("分配失败!");//,"/user/getuserlist");
  					} else {

  						layer.msg("分配成功!");//,"/user/getuserlist");
  					}
  				}
  			}); 
  	}

  function getCheckedId(){
  	   var ids = "";
  	  $("[name='checkbox']").each(function(){
  		   if($(this).is(":checked")){
  			     ids +=$(this).val()  +"," 
  		   } 
  	  })
  	 return ids
  }
  
  //重置密码的方法
  function resetpwd (ids) {
	  $.ajax({
		  type : "post",
		  url : "/user/resetuserpwd",
		  data : {ids : ids},
		  dataType : "json",
		  async:false,
		  success : function(data) {
			if (data == 0) {
				layer.msg("密码重置失败，请重试!");
			} else {
				layer.msg("密码重置成功", {icon: 1},function(){ window.location.href="/user/getuserlist";});
			}
		  }
	  });
  }

  var roleSel = "";
  function boundcld() {
  	if(roleSel.length<1){
  		
  		$.ajax({
  			type : "POST",
  			url : "/user/getrolelist",
  			data : {
  				
  			},
  			dataType : "json",
  			async:false,
  			success : function(data) {
  				for(var i=0;i<data.length;i++){
  					
  					roleSel += "<option value='"+data[i].id+"'>"+data[i].name+"</option>"
  					 
  				}
  				
  			}
  			
  		});
  	} 
  	$("#rolename").html(roleSel);
  }

  $(function(){
  	 $("#validatebound-form").validate({
  		 rules: {
  		      },
  		      messages: {
  		      },
  		 submitHandler: function(form) {
  			   uid = getCheckedId();
  				var rid = $("#rolename").val();
  				$.ajax({
  					type : "POST",
  					url : "/user/saveRole",
  					data : {
  						uid: uid,
  						rid : rid,
  					},
  					dataType : "json",
  					success : function(data) {
  						if (data == 0) {
  							layer.msg("绑定失败!");
  						} else {

  							layer.msg("绑定成功!", {icon: 1},function(){ window.location.href="/user/getuserlist";});
  						}
  					}
  				});
  			 
  	      }
  	 
  	    });
  	 
  	 
  	 $("#validateupdate-form").validate({
  		 rules: {
  		    	name: "required",
  		      },
  		      messages: {
  		    	  name: "请输入姓名",
  		      },
  		      
  		 submitHandler: function(form) {
  			 
  			    var aid = $("#aid").val();
  				var name = $("#name").val();
  				var sex = $("#sex").val();
  				var birthday = $("#birthday").val();
  				var cardid = $("#cardid").val();
  				var address = $("#address").val();
  				var education = $("#education").val();
  				var currentaddr = $("#currentaddr").val();
  				var phone = $("#phone").val();
  				var deptid=$("#deptid").val();
  				$.ajax({
  					type : "POST",
  					url : "/user/saveuserbyid",
  					data : {
  						aid: aid,
  						name : name,
  						sex : sex,
  						birthday : birthday,
  						cardid : cardid,
  						address : address,
  						education : education,
  						currentaddr : currentaddr,
  						phone : phone,
  						deptid:deptid
  					},
  					dataType : "json",
  					success : function(data) {
  						if (data == 0) {
  							layer.msg("修改失败!");
  						} else {

  							layer.msg("修改成功!",{icon: 1},function(){ window.location.href="/user/getuserlist";});
  						}
  					}
  				});
  			 
  	      }
  	 
  	    });
  })


  function updatecld(f) {
  	$.ajax({
  		type : "POST",
  		url : "/user/getuserbyid",
  		data : {
  			
  			id : f.id
  		},
  		dataType : "json",
  		success : function(data) {
  			$("#aid").val(f.id);
  			$("#name").val(data.name);
  			$("#sex").val(data.sex);
  			$("#birthday").val(data.birthday);
  			$("#cardid").val(data.cardid);
  			$("#address").val(data.address);
  			$("#education").val(data.education);
  			$("#currentaddr").val(data.currentaddr);
  			$("#phone").val(data.phone);
  			$("#deptid").val(data.deptid);
  		}
  		
  	});
  	//$("#myModal2").modal("show");
  } 

  function deleteuser() {
	  layer.confirm('您确定要删除该用户？', {
		  btn: ['确定','取消'] //按钮
		}, function(){
			 $.ajax({
	  				type : "POST",
	  				url : "/user/deleteuseraccount",
	  				data : {
	  					aids: id
	  				},
	  				dataType : "json",
	  				success : function(data) {
	  					if (data == 0) {
	  						layer.msg("删除失败!");
	  					} else {
	  						layer.msg("删除成功!", {icon: 1},function(){ window.location.href="/user/getuserlist";});
	  					}
	  				}
	  			});
		}, function(){
		}); 
  }
</script>
 
</body>

</html>